package com.nondo.dean.auth.provider.impl;

import com.nondo.dean.auth.common.BusinessUtils;
import com.nondo.dean.auth.common.bean.OfficeItem;
import com.nondo.dean.auth.common.bean.OfficeTree;
import com.nondo.dean.auth.domain.Office;
import com.nondo.dean.auth.provider.OfficeManager;
import com.nondo.dean.auth.service.OfficeService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class OfficeManagerImpl implements OfficeManager {

    @Autowired
    private OfficeService officeService;

    @Override
    public View<String> requestSave(OfficeItem item) {

        String parentPath = "";
        Office office = BusinessUtils.toOffice(item);
        if (office.getParentId() != null) {

            if (office.getParentId().equals(office.getId())) {
                return View.ofParamsError("不能修改父级部门为本部门");
            }

            //父菜单不为空，查询出父菜单
            Office parentOffice = officeService.findByOfficeId(office.getParentId());
            parentPath = parentOffice.getPath();
        }

        office.setPath(parentPath + "/" + office.getName());
        officeService.saveAndUpdateOffice(office);

        return View.ofOk();
    }

    @Override
    public View<String> requestDelete(Long officeId) {

        officeService.deleteByOfficeId(officeId);
        return View.ofOk();
    }

    @Override
    public View<String> requestMultiDelete(Long[] officeIds) {

        for (Long id : officeIds) {
            officeService.deleteByOfficeId(id);
        }
        return View.ofOk();
    }

    @Override
    public View<Page<Office>> requestQuery(Query<OfficeItem> item) {

        Query<Office> officeQuery = new Query<>();
        officeQuery.setPage(item.getPage());
        officeQuery.setSize(item.getSize());
        officeQuery.setData(BusinessUtils.toOffice(item.getData()));

        Page<Office> officePage = officeService.findOfficePage(officeQuery);
        return View.ofOk(officePage);
    }

    @Override
    public View<List<OfficeTree>> requestOfficeTreeOfAll(Long companyId) {
        List<OfficeTree> companyTreeList = new ArrayList<>();
        List<Office> companyList = officeService.findOfficeByCompanyId(companyId);

        initRootOffice(companyTreeList, companyList);

        return View.ofOk("树形结构", companyTreeList);
    }

    @Override
    public View<Office> requestOfficeOne(Long officeId) {
        Office office = officeService.findByOfficeId(officeId);
        if (office == null) {
            return View.ofError("部门信息没有找到");
        }
        return View.ofOk(office);
    }

    /**
     * 取出顶级部门
     *
     * @param officeTreeList 部门结果集合
     * @param offices        部门集合
     */
    private void initRootOffice(List<OfficeTree> officeTreeList, List<Office> offices) {
        Iterator<Office> iter = offices.iterator();
        while (iter.hasNext()) {
            Office office = iter.next();
            if (office.getParentId() == null) {
                officeTreeList.add(new OfficeTree(office.getName(), office.getName(), office.getId() + "", office.getId() + ""));
                iter.remove();
            }
        }
        initOfficeTree(officeTreeList, offices);
    }

    /**
     * 转换部门为树形部门
     *
     * @param officeTreeList 部门结果集合
     * @param offices        部门集合
     */
    private void initOfficeTree(List<OfficeTree> officeTreeList, List<Office> offices) {

        List<OfficeTree> childMenuTreeList = new ArrayList<>();
        OfficeTree treeBean;
        for (OfficeTree bean : officeTreeList) {
            Iterator<Office> iter = offices.iterator();
            while (iter.hasNext()) {
                Office office = iter.next();
                if (bean.getKey().equals(office.getParentId() + "")) {
                    treeBean = new OfficeTree(office.getName(), office.getName(), office.getId() + "", office.getId() + "");
                    bean.getChildren().add(treeBean);
                    childMenuTreeList.add(treeBean);
                    iter.remove();
                }
            }
        }
        if (!childMenuTreeList.isEmpty()) {
            initOfficeTree(childMenuTreeList, offices);
        }

    }
}
